package com.qn.crane4j;

import cn.crane4j.core.container.Container;
import com.qn.entity.User;
import com.qn.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Component // 交给 Spring 管理
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class UserContainer implements Container<Long> {
    private final UserService userService;

    @Override
    public String getNamespace() {
        return "user";
    }

    @Override
    public Map<Long, ?> get(Collection<Long> ids) {
        // 从数据库或缓存中获取用户信息
        List<User> list = userService.list();
        return list.stream()
                .collect(Collectors.toMap(User::getId, Function.identity()));
    }

}